package net.yambal.RTM.Method
{
	import flash.events.EventDispatcher;
	
	import net.yambal.RTM.Event.ContactsEvent;
	import net.yambal.RTM.Items.RTMContact;
	import net.yambal.RTM.Items.RTMTransaction;
	import net.yambal.RTM.RestWrapper.RestManager;
	import net.yambal.RTM.RestWrapper.RestService;
	import net.yambal.RTM.Utilty.Parser;
	
	[Event(name="get_list", type="net.yambal.RTM.Event.ContactsEvent")]
	[Event(name="add", type="net.yambal.RTM.Event.ContactsEvent")]
	[Event(name="delete", type="net.yambal.RTM.Event.ContactsEvent")]
	public class Contacts extends EventDispatcher
	{
		public function Contacts()
		{
		}
		
		// ------------------------------------------------------------------------------------
		/**
		 * getList
		 **/
		public function getList(api_key:String, shared_secret:String, auth_token:String):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_CONTACTS_GET_LIST, p, this.onGetList, this.onFaultGetList, false, shared_secret);
		}
		
		private function onFaultGetList(code:Number, massage:String, ro:Object):void{
			trace(code);
			trace(massage);
		}
		
		private function onGetList(result_xml:XML, ro:Object, t:RTMTransaction):void{
			var ret:Array = Parser.ContactXMMLListToContact(result_xml.descendants("contact"));
			this.dispatchEvent(new ContactsEvent(ContactsEvent.GET_LIST, ret));
		}
		
		// ------------------------------------------------------------------------------------
		/**
		 * add
		 **/
		public function add(api_key:String, shared_secret:String, auth_token:String, email:String):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.contact = email;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_CONTACTS_ADD, p, this.onAdd, this.onFaltAdd, true, shared_secret);
		}
		private function onFaltAdd(code:Number, massage:String, ro:Object):void{
			trace(code);
			trace(massage);
		}
		
		private function onAdd(result_xml:XML, ro:Object, t:RTMTransaction):void{
			var ret:RTMContact = Parser.ContactXMMLToContact(XMLList(result_xml.descendants("contact"))[0]);
			this.dispatchEvent(new ContactsEvent(ContactsEvent.ADD, null, ret, t));
		}
		
		// ------------------------------------------------------------------------------------
		/**
		 * delete
		 **/
		public function delete_contact(api_key:String, shared_secret:String, auth_token:String, contact:RTMContact):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.contact_id = contact.id;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_CONTACTS_DELETE, p, this.onDelete, this.onFaltDelete, true, shared_secret);
		}
		
		private function onFaltDelete(code:Number, massage:String, ro:Object):void{
			trace(code);
			trace(massage);
		}
		
		private function onDelete(result_xml:XML, ro:Object, t:RTMTransaction):void{
			var ret:RTMContact = Parser.ContactXMMLToContact(XMLList(result_xml.descendants("contact"))[0]);
			this.dispatchEvent(new ContactsEvent(ContactsEvent.DELETE, null, null, t));
		}
	}
}